//
//  NSString+Chinese.m
//  ICome
//
//  Created by Administrator on 2017/5/11.
//  Copyright © 2017年 iCom. All rights reserved.
//

#import "NSString+Chinese.h"

@implementation NSString (Chinese)


- (NSString *)obtainCharacter
{
    NSString *headerName = nil;
    if(self.length >1){
        NSString *last = [self substringFromIndex:self.length-1];//字符串结尾
        if([last isChinese])
        {
            headerName = last;
        }else{
            NSString *first = [self substringToIndex:2];//字符串开始
            if([first includeChinese]){
                for (int i=0; i<[first length]; i++)
                {
                    int a = [first characterAtIndex:i];
                    if (a < 0x9fff && a > 0x4e00)
                    {
                        headerName = [first substringWithRange:NSMakeRange(i, 1)];
                    }
                }
            }else{
                headerName = first;
            }
            
        }
    }else{
        headerName = self;
    }
    
  return headerName;

}

- (BOOL)isChinese
{
    NSString *match = @"(^[\u4e00-\u9fa5]+$)";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF matches %@", match];
    return [predicate evaluateWithObject:self];
}

- (BOOL)includeChinese
{
    for(int i=0; i< [self length];i++)
    {
        int a =[self characterAtIndex:i];
        if( a >0x4e00&& a <0x9fff){
            return YES;
        }
    }
    return NO;
}

@end
